/**
 * 
 */
package eu.smoothit.sis.db.impl.daos;

import java.util.List;

import org.apache.log4j.Logger;

import eu.smoothit.sis.db.api.daos.IPeerStatusDAO;
import eu.smoothit.sis.db.impl.entities.PeerStatusEntry;
import eu.smoothit.sis.db.impl.utils.IPersistenceManager;

/**
 * @author christian
 * 
 */
public class PersistentPeerStatusDAO extends JpaDao<Long, PeerStatusEntry>
		implements IPeerStatusDAO {

	private final static Logger logger = Logger
			.getLogger(PersistentComponentConfigDAO.class);

	private static String peerStatusTable = "PeerStatusEntry";

	public PersistentPeerStatusDAO(IPersistenceManager persistenceManager) {
		super(persistenceManager);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<PeerStatusEntry> get(PeerStatusEntry entry) {
		String conditions = createQueryConditionsFromParameters(entry);
		String query = "SELECT h FROM " + peerStatusTable + " h" + conditions;
		logger.info("Resulting query of getProperties()" + query);
		return pm.queryDatabase(query);
	}

	private String createQueryConditionsFromParameters(PeerStatusEntry entry) {
		StringBuffer ret = new StringBuffer();

		if (entry.getDown_total() != null) {
			ret.append(" h.down_total=" + entry.getDown_total() + " AND");
		}
		if (entry.getFilename() != null) {
			ret.append(" h.filename='" + entry.getFilename() + "' AND");
		}
		if (entry.getInfohash() != null) {
			ret.append(" h.infohash='" + entry.getInfohash() + "' AND");
		}
		if (entry.getListenport() != null) {
			ret.append(" h.listenport=" + entry.getListenport() + " AND");
		}
		if (entry.getLive() != null) {
			ret.append(" h.live=" + entry.getLive() + " AND");
		}
		if (entry.getP_dropped() != null) {
			ret.append(" h.p_dropped=" + entry.getP_dropped() + " AND");
		}
		if (entry.getP_late() != null) {
			ret.append(" h.p_late=" + entry.getP_late() + " AND");
		}
		if (entry.getP_played() != null) {
			ret.append(" h.p_played=" + entry.getP_played() + " AND");
		}
		if (entry.getPeer_id() != null) {
			ret.append(" h.peer_id='" + entry.getPeer_id() + "' AND");
		}
		if (entry.getProgress() != null) {
			ret.append(" h.progress=" + entry.getProgress() + " AND");
		}
		if (entry.getStatus() != null) {
			ret.append(" h.status='" + entry.getStatus() + "' AND");
		}
		if (entry.getT_prebuf() != null) {
			ret.append(" h.t_prebuf=" + entry.getT_prebuf() + " AND");
		}
		if (entry.getT_stalled() != null) {
			ret.append(" h.t_stalled=" + entry.getT_stalled() + " AND");
		}
		if (entry.getTimestamp() != null) {
			ret.append(" h.timestamp=" + entry.getTimestamp() + " AND");
		}
		if (entry.getUp_rate() != null) {
			ret.append(" h.up_rate=" + entry.getUp_rate() + " AND");
		}
		if (entry.getUp_total() != null) {
			ret.append(" h.up_total=" + entry.getUp_total() + " AND");
		}
		if (entry.getDown_rate() != null) {
			ret.append(" h.down_rate=" + entry.getDown_rate() + " AND");
		}
		if (entry.getIop_flag() != null) {
			ret.append(" h.iop_flag=" + entry.getIop_flag() + " AND");
		}
		if (entry.getValidRange() != null) {
			ret.append(" h.validRange='" + entry.getValidRange() + "' AND");
		}
		String string = ret.toString();
		if (string.endsWith("AND")) {
			string = string.substring(0, string.length() - 4);
			return " WHERE" + string;
		}
		return string;
	}

}
